package com.hzncc.scaffold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Banner;
import com.hzncc.scaffold.mapper.BannerMapper;
import com.hzncc.scaffold.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 横幅表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerService bannerService;


    @Override
    public List<Banner> queryByEntity(Banner banner) {
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<>();
        lqw.eq(banner.getId() != null, Banner::getId, banner.getId());
        lqw.eq(banner.getImg() != null, Banner::getImg, banner.getImg());
        lqw.eq(banner.getLink() != null, Banner::getLink, banner.getLink());
        lqw.eq(banner.getModule() != null, Banner::getModule, banner.getModule());
        lqw.eq(banner.getStatus() != null, Banner::getStatus, banner.getStatus());
        lqw.eq(banner.getPubTime() != null, Banner::getPubTime, banner.getPubTime());
        lqw.eq(banner.getSort() != null, Banner::getSort, banner.getSort());
        lqw.eq(banner.getCreateTime() != null, Banner::getCreateTime, banner.getCreateTime());

        List<Banner> bannerList = bannerService.list(lqw);
        return bannerList;
    }

    @Override
    public ResultT saveBanner(Banner banner) {
        boolean save = bannerService.save(banner);
        if(save){
            return ResultT.success("新增成功");
        }else {
            return ResultT.fail("新增失败");
        }
    }

    @Override
    public ResultT updateBanner(Banner banner) {
        boolean update = bannerService.updateById(banner);
        if(update){
            return ResultT.success("修改成功");
        }else{
            return ResultT.fail("修改失败");
        }
    }

    @Override
    public ResultT moveBanner(Integer curPos, Integer targetPos, String key) {
        if(curPos!=null && targetPos!=null){
            Banner curPosBanner = new Banner();
            Banner targetPosBanner = new Banner();

            curPosBanner.setSort(curPos);
            targetPosBanner.setSort(targetPos);

            curPosBanner = bannerService.queryByEntity(curPosBanner).get(0);
            targetPosBanner = bannerService.queryByEntity(targetPosBanner).get(0);

            curPosBanner.setSort(targetPos);
            targetPosBanner.setSort(curPos);

            bannerService.updateById(curPosBanner);
            bannerService.updateById(targetPosBanner);

            return ResultT.success("操作成功");
        }else {
            return ResultT.fail("信息错误");
        }
    }
}
